package com.totoro.test;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.totoro.exception.ResultCode;
import com.totoro.exception.TmallProcessErrorMap;
import com.totoro.pojo.SuMainChat;
import com.totoro.pojo.SuperEvaluationFind;
import com.totoro.utils.PhoneUtil;
import com.totoro.utils.TotoroUtils;
import com.totoro.utils.WechatTimeUtil;
import lombok.SneakyThrows;
import org.junit.Test;
import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;
import java.lang.reflect.Field;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.Calendar;

/**
 * @author TOTORO
 * @date 2021/7/15 19:04
 */
public class Demo1 {

    @SneakyThrows
    @Test
    public void ts() {
        String json = "[{\"evaluateType\":\"1\"},{\"waitingToBP\":\"all\"},{\"haveAppend\":\"true\"},{\"filterItemId\":\"123\"},{\"filterBuyerNick\":\"123\"},{\"filterDateStart\":\"2021-07-01\"},{\"filterDateEnd\":\"2021-07-16\"}]";
//        手动获取所有的键。。。
        String[] splits = json.replaceAll("\\[", "").replaceAll("]", "").replaceAll("\\{", "").replaceAll("}", "").replaceAll("\"", "").split(",");
        String[] keys = new String[splits.length];
        for (int i = 0; i < splits.length; i++) {
            keys[i] = splits[i].split(":")[0];
        }
        Map<String, Object> mapperMap = new HashMap<>();
        mapperMap.put("true", 1);
        mapperMap.put("false", 0);
        mapperMap.put("all", null);
        mapperMap.put("sellerExplain", 0);
        mapperMap.put("9999", null);

        // 反射设置属性值
        SuperEvaluationFind model = new SuperEvaluationFind();
        ObjectMapper mapper = new ObjectMapper();
        JsonNode root = mapper.readTree(json);
        for (int i = 0; i < root.size(); i++) {
            for (int j = 0; j < keys.length; j++) {
                if (root.get(i).get(keys[j]) != null) {
                    Field field = model.getClass().getDeclaredField(keys[j]);
                    field.setAccessible(true);
                    if (mapperMap.containsKey(root.get(i).get(keys[j]).asText())){
                        field.set(model, mapperMap.get(root.get(i).get(keys[j]).asText()));
                    } else{
                        field.set(model, root.get(i).get(keys[j]).asText());
                    }
                }
            }
        }
        model.setSlid(1);
        model.setIsTerm(1);
        System.out.println(model);
    }

    private List<String> findKeys(Map<String, Object> treeMap , List<String> keys) {
        treeMap.forEach((key, value) -> {
            if (value instanceof LinkedHashMap) {
                Map<String, Object> map = (LinkedHashMap) value;
                findKeys(map, keys);
            }
            keys.add(key);
        });

        return keys;
    }

    @SneakyThrows
    @Test
    public void demo3(){
        PhoneUtil.getPhoneCode("15200435543", 123456);
    }


}
